<?php
/*
 * 进云智能生成平台
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
 */

defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'reply';
$return=array();
//查询当前粉丝是否关注当前公众号
$fans = cfc('core')->get_fans($_SESSION['openid']);
$fansinfo = pdo_get('core_fans',array('id'=>$fans['id'],'uniacid'=>$_SESSION['uniacid']));
$follow = $fansinfo['follow'];
$backurl=$_RQ['backurl']?$_RQ['backurl'].'/index/':'core/home.welcome/';
$backurl=custom_url($backurl);
if($_RQ['op']=='main'){
	if($_RQ['id']){
		$id=intval($_RQ['id']);
	}
	if(!$id){
		api_exi('参数错误！','error');
	}
	$item=pdo_get('jy_h5game_game',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));	
	if(!$item){
		api_exi('指定的活动模板不存在！');
	}
}
if($_RQ['op']=='reply'){
	$_SESSION['account']['logo']=$_SESSION['account']['logo']?:'../core/resource/images/nopic.jpg';
	$_SESSION['account']['ewm']=$_SESSION['account']['ewm']?:'../core/resource/images/nopic.jpg';
	//来自商城活动
	$wait_id=intval($_RQ['wait_id']);
	if($wait_id){
		$id=$this->model()->check_game($wait_id);
	}elseif($_RQ['id']){
		$id=intval($_RQ['id']);
		$_SESSION['action_id']=$id;
		$this->session('rid',$id);
		$e=$id;
		$wait_id=$this->model()->is_shopgame($id);
	}
	if(!$id){
		api_exi('参数错误！','error');
	}
	 //查询对应活动的参数
	$item=pdo_get('jy_h5game_reply',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$game=pdo_get('core_h5game',array('name'=>$item['fileName']));	
	//获取当前时间
	$time = time();
	//玩家是否被拉黑
	$shielding = pdo_getcolumn('jy_h5game_fans',array('fansId'=>$fansinfo['mid'],'uniacid'=>$_SESSION['uniacid']),'shielding');
	if(!$item){
		exi('指定的活动不存在！','error',$backurl);
	}
	if($shielding == 2){
	  	api_exi('抱歉，您被限制参与该活动','error',$backurl);
	}
	if($item['status'] == 0){
		exi('活动尚未发布','error',$backurl);
	}
	$item['backpicurl']=$item['backpicurl']?tomedia($item['backpicurl']):'';
	$item['starttime2']=date('Y-m-d H:m:s',$item['starttime']);
	$item['endtime2']=date('Y-m-d H:m:s',$item['endtime']);
	if($_SESSION['os']=='mobile' || $_RQ['os']=='mobile'){	
		/*if($item['status'] == 3){
			exi('活动已结束','error');
		}
		*/
		//与开始时间、结束时间作比较
		if($time<$item['starttime']){
			exi('活动还未开始，请于'.$item['starttime'].'后参与','error','jy_h5game/index/');
		}
		if($time>$item['endtime']){
			pdo_update('jy_h5game_reply',array('status'=>'3'),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
			exi('活动已结束','error',$backurl);
		}
		$game_condition_file=$this->plugin_root.'/custom/templates/mobile/games/'.$game['name'].'/condition.php';
		if(is_file($game_condition_file)){
			require($game_condition_file);
		}
		
		//参与该活动的人数
		 $playerNum = pdo_count('jy_h5game_fans',array('rid'=>$id,'uniacid'=>$_SESSION['uniacid']));
		 //总人数=实际人数+虚拟人数
		 $playerNums = $playerNum + $item['virtualRegNum'];
		 //判断玩家是否在玩家表中
		 $isInList = pdo_get('jy_h5game_fans',array('fansID'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'rid'=>$id));
		 //参与活动人数限制
		 if($item['isLimitRegNum'] == 1){
			 if($item['virtualRegNum_new'] && $playerNum>=$item['virtualRegNum_new'] && !$isInList){
				 api_exi('抱歉，活动人数已满','error');
			 }
		 }
		if($time>=$item['starttime']&&$time<=$item['endtime']&&$item['status']== 2){
			$check=$this->getcolumn('fans',array('fansId'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'rid'=>$id),'id');
			if(!$check){
				if($_RQ['fromuser']){
					$fromchannel='share';
				}elseif($_RQ['fromsao']){
					$fromchannel='sao';
				}else{
					$fromchannel='url';
				}
				$insert=array(
					'rid'=>$id,
					'fansId'=>$_SESSION['uid'],
					'headImg'=>$fansinfo['avatar'],
					'name'=>$fansinfo['nickname'],
					'uniacid'=>$_SESSION['uniacid'],
					'createtime'=>TIMESTAMP,
					'bestScore'=>0,
					'fromchannel'=>$fromchannel
				);
				$this->insert('fans',$insert);
			}
		}
		if($item['fileName']=='hd_sjh' || $item['fileName']=='h5_sq' || $item['fileName']=='hd_sqdm' ||  $item['fileName']=='hd_ddp'){
			$return['fansinfo']['avatar']=$fansinfo['avatar'];
			$return['fansinfo']['nickname']=$fansinfo['nickname'];
			$return['fansinfo']['sex']=$fansinfo['sex'];
			$return['htmls']=$this->template_url('games/'.$item['fileName'].'/index');
			exi($return);
		 }
		 //排行榜url
		 $url = './index.php?plugin=jy_h5game&action=rankings&fansID='.$_SESSION['uid'].'&rid='.$id;
		 $infourl = './index.php?plugin=jy_h5game&action=info&fansID='.$_SESSION['uid'].'&id='.$id;
		 $awardurl = './index.php?plugin=jy_h5game&action=award&fansID='.$_SESSION['uid'].'&id='.$id;
		 $noticeurl = './index.php?plugin=jy_h5game&action=notice&fansID='.$_SESSION['uid'].'&id='.$id;
		 $welcomeurl = './index.php?plugin=jy_h5game&action=welcome&fansID='.$_SESSION['uid'].'&id='.$id;
		 
		 //玩家最高得分
		 $bestScore = pdo_getcolumn('jy_h5game_fans',array('fansID'=>$fansinfo['mid'],'uniacid'=>$_SESSION['uniacid'],'rid'=>$id),'bestScore');
		 //获取公众号信息
		 $account = $_SESSION['account'];
		 $ewm = pdo_getcolumn('core_accounts',array('uniacid'=>$_SESSION['uniacid']),'ewm');
		//获取token
		$token = $_SESSION['token'];
		 //活动奖项数据 转成数组格式
		 $award = json_decode($item['award'],true);
		 if($item['fileName']=='hd_bigwheel'){
			//$award['7'] = array("prizeName"=>"谢谢惠顾","prizeLevel"=>"","prizeNum"=>"");
		 }
		 foreach($award as &$av){
			 $av['fee'] = rand($av['hbLow']*100,$av['hbHigh']*100)/100;
		 }
		//获取游戏模板
		 $file = '../'.ADDONS.'/jy_h5game/custom/templates/mobile/games/'.$item['fileName'];
		//$return['html']='../'.ADDONS.'/jy_h5game/custom/templates/mobile/'.$item['fileName'].'/index.html';
		 //用户可提现金额
		$money = pdo_getsum('jy_h5game_award',array('rid'=>$id,'uniacid'=>$_SESSION['uniacid'],'fansID'=>$_SESSION['uid'],'awardstatus'=>0),'awardhb');
		//读取jssdk
		$jssdk=cfc('account',$_AR['wx']['id'])->getJssdkConfig();
		$jssdk=ijson_encode($jssdk);
		$share=array(
			'title'=>$item['title'] ? $item['title'] : $item['account']['title'],
			'desc'=>$item['share'] ? $item['share'] : $item['title'],
			'link'=>custom_url('jy_h5game/welcome//id='.$item['id'].'&fromuser='.$_SESSION['uid'],true),
			'imgUrl'=>$item['logo'] ? tomedia($item['logo']) : $_AR['siteroot'].'core/resource/images/nopic.jpg',
		);		
		$share=ijson_encode($share);			
		//查询玩家中奖次数
		$awardTimes = pdo_count('jy_h5game_award',array('rid'=>$id,'uniacid'=>$_SESSION['uniacid'],'fansID'=>$fansinfo['mid']));
		//剩余抽奖次数
		$item['istotalnum']=$isInList['totalnum'];
		$leftPlayCount=$this->model()->get_lasttimes($item,$wait_id);
		//中奖玩家
		$awardlist = pdo_getall('jy_h5game_award',array('rid'=>$id,'uniacid'=>$_SESSION['uniacid']));
		//中奖玩家按时间顺序排序
		$aplist = pdo_getall('jy_h5game_award',array('rid'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']),'','','createtime desc');			
		if($aplist){				
			foreach($aplist as &$row){		
				$row['headImg'] = pdo_getcolumn('jy_h5game_fans',array('rid'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid'],'fansID'=>$row['fansID']),'headImg');
			}
		}
		$game_php_file=$this->plugin_root.'/custom/templates/mobile/games/'.$game['name'].'/index.php';
		if(is_file($game_php_file)){
			require($game_php_file);
		}
		//判断活动名额和状态
		$jsondata=$this->model()->get_checkgame($item,$wait_id);
		$json = urldecode(json_encode($jsondata));
		$reply=$item;
		$jssdkconfig=cfc('account')->getJssdkConfig();
		$member = pdo_getall('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('realname','mobile','address'));
		include $this->template('games/'.$item['fileName'].'/index',$_RQ['os']);
	}else{
		$return['list']=pdo_getall('jy_h5game_fans',array('rid'=>$id,'uniacid'=>$_SESSION['uniacid'],'awardnum'=>1),array('headImg','name','awardinfo'),'','awardinfo asc');
		$return['jx']=json_decode($item['award'],true);
		$return['item']=array();
		$return['rid']=$id;
		$game_pc_file=$this->plugin_root.'/custom/templates/mobile/games/'.$game['name'].'/pc_game.php';
		if(is_file($game_pc_file)){
				require($game_pc_file);
		}
		$game_pc_html=$this->plugin_root.'/custom/templates/mobile/games/'.$game['name'].'/index1.html';
		if(is_file($game_pc_html)){
			$return['htmls']=$this->template_url('games/'.$item['fileName'].'/index1');
		}else{
			$return['htmls']=$this->template_url('games/'.$item['fileName'].'/index');
		}
		set_html('title','活动详情');
		exi($return);
	}
}
if($_RQ['op']=='sjh'){
		$post=get_postdata();
		if(!$post['tel']){
					exi('请输入手机号！','error');
		}
		if($post){
				pdo_update('jy_h5game_fans',array('tel'=>$post['tel']),array('fansId'=>$_SESSION['uid'],'rid'=>$_SESSION['action_id']));
				exi('提交成功','success','/welcome/reply/id='.$_SESSION['action_id']);
		}
}
if($_RQ['op']=='ddp'){
	$post=get_postdata();
	if($post){
			pdo_update('jy_h5game_fans',array('sex'=>$post['sex']),array('fansId'=>$_SESSION['uid'],'rid'=>$_SESSION['action_id']));
			exi('提交成功','success','/welcome/reply/id='.$_SESSION['action_id']);
	}else{
		exi('请选择性别！','error');
	}
}
if($_RQ['op']=='xc'){
	$postdata=get_postdata();
	if($_RQ['postdata']){
		$postdata=get_postdata();
		if(!$postdata['image']){
				exi('图片不能为空！','error');
		}
		$update=array(
			'image'=>trim($postdata['image']),
		);
		pdo_update('jy_h5game_fans',$update,array('fansId'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'rid'=>$postdata['rid']));
		exi('保存成功','success','/welcome/reply/id='.$_SESSION['action_id']);
		
	}
}
//保存玩家最高分
if($_RQ['op']=='bs'){
	$bs = $_RQ['bs'];
	$id = $_RQ['id'];
	$z = array();
	$z['id'] = $id;
    $z['bs'] = $bs;	
	$bestScore = pdo_getcolumn('jy_h5game_fans',array('fansId'=>$fansinfo['mid'],'uniacid'=>$_SESSION['uniacid'],'rid'=>$id),'bestScore');
	if($bs>$bestScore){
		pdo_update('jy_h5game_fans',array('bestScore'=>$bs),array('fansId'=>$fansinfo['mid'],'uniacid'=>$_SESSION['uniacid'],'rid'=>$id));		
	}	
	$return['bs'] = $bs;
	$return['id'] = $id;
	$return['bestScore'] = $bestScore;
	exi($return);
}
if($_RQ['op']=='api'){
	$game_php_file=$this->plugin_root.'custom/templates/mobile/games/'.$game['name'].'/index.php';
	if(is_file($game_php_file)){
		require($game_php_file);
	}
	exi('该小游戏没有相应数据获取接口！','error');
}


